package com.example.web1.web;

import com.example.web1.dao.User;
import com.example.web1.dao.User2;
import com.example.web1.entity.conUser;
import com.example.web1.entity.webUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("index")
@Controller
public class Index {
    @Autowired
    User user;

    @RequestMapping("list")
    @ResponseBody
    public List<webUser> list() {
        List<webUser> list = user.list();
        return list;
    }

    @RequestMapping("indexhtml")
    public String listhtml(ModelMap map) {
        List<webUser> list = user.list();
        map.put("userList", list);
        for (webUser user : list) {
            System.out.println(user.getName());
            System.out.println(user.getGDP());
            System.out.println(user.getCapital());
            System.out.println(user.getPeople());
        }
        return "index";
    }

    @RequestMapping("addhtml")
    public String save() {
        return "add";
    }

    @PostMapping("add")
    public String add(webUser u) {
        user.add(u);
        return "redirect:/index/indexhtml";
    }

    @RequestMapping("delete/{name}")
    public String delete(@PathVariable("name") String xxx) {
        user.delete(xxx);
        return "redirect:/index/indexhtml";
    }

    @GetMapping("update/{name}")
    public String update(@PathVariable("name") String name, ModelMap map) {
        map.put("user", user.web(name));
        return "/update";
    }

    @PostMapping("update")
    public String update(webUser u) {
        user.update(u);
        return "redirect:/index/indexhtml";
    }
}